<template>
  <div class="seasonsChange">
    <!-- 季度转换 -->
    <div class="new_vs">
      <div class="vs-top">
        <p>季度转换</p>
        <!-- <i class="el-icon-close"></i> -->
      </div>
      <div class="msg">
        <p>
          当前季节 <span style="color: red">({{ quarterTitle }})</span>
        </p>
        <p>请选择转换的季节</p>
        <div style="margin: 10px 0">
          <el-radio v-model="radio" label="1">春夏</el-radio>
          <el-radio v-model="radio" label="2">秋冬</el-radio>
        </div>

        <el-select
          v-model="value"
          style="margin-right: 10px"
          @change="selected"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-input v-model="input" disabled></el-input>
      </div>
      <div class="vs-btn">
        <!-- <el-button>取消</el-button> -->
        <el-button @click="determine" type="primary">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getQuarter,
  postQuarter,
  changeQuarter,
  quarterChange,
  getQuarterShow
} from '../../../network/quarter'
import { logOut } from '@/network/login'

export default {
  data() {
    return {
      radio: '1',
      value: '',
      options: [],
      input: '',
      // jxQuarter: document.cookie.match(RegExp('jxQuarter' + '=([^;]+)'))[1],
      jxQuarter: null,
      quarterList: [],
      quarterTitle: '暂无季节',
      tempData: []
    }
  },
  created() {
    let jxQuarterVal = document.cookie.match(RegExp('jxQuarter' + '=([^;]+)'))
    this.jxQuarter = jxQuarterVal ? jxQuarterVal[1] : null

    let year = new Date().getFullYear()
    const NextYear = new Date().getFullYear() + 1
    for (let i = 0; i < 5; i++) {
      this.options.push({ value: year, label: year-- })
    }
    this.options.unshift({
      value: NextYear,
      label: NextYear
    })
    if (this.jxQuarter) {
      this.value = '20' + this.jxQuarter.substr(0, 2)
      this.input = this.jxQuarter
      if (this.jxQuarter.substr(2, 2) == '春夏') {
        this.radio = '1'
      } else {
        this.radio = '2'
      }
    }
    getQuarter().then((res) => {
      this.quarterList = res.data.data
    })

    this.getQuarterShow()
  },

  watch: {
    radio(a, b) {
      if (this.value) {
        this.input =
          this.value.toString().substring(2, 4) + (a == '1' ? '春夏' : '秋冬')
      }
    }
  },
  methods: {
    async getQuarterShow() {
      this.tempData = []
      const { data } = await getQuarterShow()
      if (data && data.code === 200) {
        this.quarterTitle = data.data || '暂无季节'
        if (data.data) {
          let seasonYear = this.quarterTitle.slice(0, 2)
          let seasonTitle = this.quarterTitle.slice(2, 4)
          this.radio = seasonTitle === '春夏' ? '1' : '2'
          this.value = Number(`20${seasonYear}`)
          this.tempData.push(this.value)
          this.tempData.push(this.radio)
        }
      }
    },

    selected(e) {
      this.input =
        e.toString().substring(2, 4) + (this.radio == 1 ? '春夏' : '秋冬')
    },
    determine() {
      let radioState = this.tempData.includes(this.radio)
      let valueState = this.tempData.includes(this.value)
      if (!radioState || !valueState) {
        if (!this.input) {
          return
        }
        quarterChange({ text: this.input }).then((res) => {
          console.log(res)
          if (res.data.code === 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })

            if (!this.quarterList.length) {
              // postQuarter({ quarterName: this.input })
            } else {
              let exist = 0
              for (const i of this.quarterList) {
                if (i.name == this.input) {
                  exist = 1
                  break
                }
              }
              // if (!exist) {
              //   postQuarter({ quarterName: this.input })
              // }
            }
            let date = new Date()
            date.setDate(date.getDate() + 30)
            document.cookie =
              'jxQuarter' + '=' + this.input + ';expires=' + date
            this.$message({
              message: '季节转换成功，需要退出重新登录',
              type: 'success',
              duration: 3000
            })
            setTimeout(() => {
              window.location.href = '/login'
            }, 2000)
            this.LogOut()
          } else {
            this.$message.error('修改失败')
          }
        })
      }
    }
  }
}
</script>

<style scoped>
.seasonsChange {
  margin-right: 0.5vw;
  padding: 0 15px;
  background-color: white;
  height: 95vh;
  position: relative;
}
.new_vs {
  position: fixed;
  top: 25%;
  left: 50%;
  transform: translateX(-50%);
  width: 350px;
  padding-bottom: 10px;
  z-index: 99;
  background-color: white;
  box-shadow: 0 0 5px #aaaaaa;
}
.new_vs .vs-top {
  height: 40px;
  background-color: #f8f8f8;
}
.new_vs .vs-top p {
  font-weight: 700;
  float: left;
  line-height: 40px;
  padding-left: 20px;
}
.new_vs .vs-top i {
  float: right;
  line-height: 40px;
  padding: 0 16px;
}
.msg {
  padding: 10px;
  margin-bottom: 10px;
}
.vs-btn {
  margin-left: 280px;
}
</style>

<style lang="less">
.seasonsChange .el-input__icon {
  line-height: 21px;
}
.seasonsChange .el-input {
  display: inline;
}
.seasonsChange .el-input__inner {
  height: 32px;
  width: 160px;
}
</style>
